<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: zhangjx
 * @Date: 2023-05-29 17:41:52
 * @LastEditors: zhangjx
 * @LastEditTime: 2023-06-08 14:55:22
-->
<template>
	<div>
		<AplidForm v-bind="formConfig" v-model="formModel" @submit="onSubmit">
			<template v-slot:administrationPidForm>
				<AplidTreeSelect
					filterLabel="administrationName"
					:props="defaultProps"
					:data="areaTreeData"
					v-model="formModel.administrationPid"
				></AplidTreeSelect>
			</template>
		</AplidForm>
	</div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod/index'
import { AplidTreeSelect } from '@/components/AplidCrud'
import { useCrudStore } from '@/stores/crud'
import { storeToRefs } from 'pinia'
const { areaTreeData } = storeToRefs(useCrudStore())
const emit = defineEmits(['refresh'])
const props = defineProps({
	nowRowData: Object,
	id: [String, Number],
})
const formModel: any = ref()

watch(
	() => props.id,
	() => {
		const data = props.nowRowData || {}
		formModel.value = data
	},
	{
		immediate: true,
	}
)
const defaultProps = {
	value: 'administrationId',
	label: 'administrationName',
	children: 'children',
}

const formConfig = {
	label: '地区信息',
	inline: false,
	showBtn: true,
	showReset: false,
	labelPosition: 'right',
	labelWidth: '125px',
	column: [
		{
			label: '行政区划名称',
			prop: 'administrationName',
			type: 'input',
			span: 24,
			rules: [{ required: true, message: '请输入行政区划名称' }],
		},
		{
			label: '上级行政区划',
			prop: 'administrationPid',
			type: 'input',
			span: 24,
			rules: [{ required: true, message: '请选择上级行政区划' }],
		},
		{
			label: '行政区划排序',
			prop: 'administrationSorting',
			type: 'input',
			tip: '数字越小，排序靠上',
			span: 24,
			rules: [{ required: true, message: '请输入行政区划排序' }],
		},
		{
			label: '行政区划编号',
			prop: 'administrationCode',
			type: 'input',
			span: 24,
			rules: [{ required: true, message: '请输入行政区划编号' }],
		},
		{
			label: '邮政编码',
			prop: 'postalCode',
			type: 'input',
			span: 24,
			rules: [{ required: true, message: '请输入邮政编码' }],
		},
	],
}

const onSubmit = (form, done) => {
	let baseApi = form.id ? 'admin_sysadministration_edit' : 'admin_sysadministration_add'
	apiMod[baseApi](form)
		.then((res) => {
			emit('refresh', {
				administrationId: res.data,
				administrationName: form.administrationName,
			})
			done()
		})
		.catch(() => {
			done()
		})
}
</script>

<style scoped></style>
